# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        fp, bp = head, head
        i = 1
        while i < n and bp:
            bp = bp.next
            i += 1
        pp = fp
        while bp.next:
            bp = bp.next
            pp = fp
            fp = fp.next
        if pp == fp:
            head = head.next
        else:
            pp.next = fp.next
        return head
